package com.yunhe.sms.service.activite.impl;

import com.yunhe.commens.util.TimeUtils;
import com.yunhe.sms.dao.activite.VoteDao;
import com.yunhe.sms.dao.activite.VoteLogDao;
import com.yunhe.sms.dao.activite.impl.VoteDaoImpl;
import com.yunhe.sms.dao.activite.impl.VoteLogDaoImpl;
import com.yunhe.sms.entity.activity.Vote;
import com.yunhe.sms.entity.activity.VoteLog;
import com.yunhe.sms.service.activite.VoteLogService;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author LeiPeifeng
 * @version V1.0
 * @date 2017/11/2 0002 19:42
 * @Description:
 */
public class VoteLogServiceImpl implements VoteLogService{
private VoteLogDao voteLogDao=new VoteLogDaoImpl ();
private VoteDao voteDao = new VoteDaoImpl ();
    @Override
    public void add(VoteLog voteLog) {
        voteLogDao.add ( voteLog );
    }

    @Override
    public void deleted(Long id) {
        voteLogDao.deleted ( id );
    }

    @Override
    public int find(VoteLog voteLog) {
        int code=1;
        Vote vote = voteDao.findId ( voteLog.getVoteId () );//获取活动信息
        String beginTime = vote.getBeginTime ();
        String endTime = vote.getEndTime ();
        Long begin = TimeUtils.StrToDateLong ( beginTime, TimeUtils.FORMAT_YYYY_MM_DD_HH_MM_SS ).getTime ();
        Long end= TimeUtils.StrToDateLong ( endTime, TimeUtils.FORMAT_YYYY_MM_DD_HH_MM_SS_MS ).getTime ();
        long currtime = System.currentTimeMillis ();
        if (currtime>end){
            code=2;//投票已经结束
        }else if (currtime<begin){
            code=3;//投票尚未开始
        }else{
            VoteLog voteLog1 = voteLogDao.find (voteLog.getVoteId () , voteLog.getMac (),voteLog.getDate ());//判断当前客户端是否投过
            if (null != voteLog1 ){
                code=4;//已经投过
            }else{
              voteLogDao.add ( voteLog ); // 没投过，存入信息
            }
        }
        return code;
    }

}
